//
//  HttpBaseModel.m
//  RuiBenO2O
//
//  Created by LiYinDi on 14-10-14.
//  Copyright (c) 2014年 RuiBen. All rights reserved.
//

#import "HttpBaseModel.h"
#import "AFNetworking.h"
AFHTTPRequestOperationManager *manager;
@implementation HttpBaseModel
+(void)severRequest:(NSString *)path params:(NSMutableDictionary *)params onCompletion:(void(^)(NSDictionary *responseData))completionBlock onError:(void(^)(NSString *error))errorBlock
{
   manager =[AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    //manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];

    NSString *url=[NSString stringWithFormat:@"%@%@",[Global getLoginUrl],path];
        [manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
         //   NSLog(@"url######===%@",url);
        NSDictionary *responseDict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        completionBlock(responseDict);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        errorBlock(error.localizedDescription);
        // NSLog(@"failure operation==%@",error);
    }];
}
+(void)stopRequest
{
    
   // AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    [manager.operationQueue cancelAllOperations];
}
+(void)severBarCodeRequest:(NSString *)path params:(NSMutableDictionary *)params onCompletion:(void(^)(NSDictionary *responseData))completionBlock onError:(void(^)(NSString *error))errorBlock
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    //manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    NSString *url=[NSString stringWithFormat:@"%@%@",[Global getLoginUrl],path];
    [manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //  NSLog(@"http######===%@",operation.responseString);
        NSDictionary *responseDict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        completionBlock(responseDict);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        errorBlock(error.localizedDescription);
//        NSLog(@"failure operation==%@",error);
    }];
}
+(void)severLoginRequest:(NSString *)path params:(NSMutableDictionary *)params onCompletion:(void(^)(NSDictionary *responseData))completionBlock onError:(void(^)(NSString *error))errorBlock
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    //manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    NSString *url=[NSString stringWithFormat:@"%@%@",MainURL2,path];
    [manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //  NSLog(@"http######===%@",operation.responseString);
        NSDictionary *responseDict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        completionBlock(responseDict);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        errorBlock(error.localizedDescription);
        //        NSLog(@"failure operation==%@",error);
    }];

}

@end
